package deweb.cimaplanning.beans;

import java.util.ArrayList;

import deweb.cimaplanning.dao.CostsPlantsDao;
import deweb.cimaplanning.dto.CostsPlants;

public class CostsPlantsBean {
	private CostsPlantsDao dao = null;
	
	public CostsPlantsBean(){
		dao = new CostsPlantsDao();
	}
	
	/**
	 * Costos por planta - mes de la versión por tipo de maíz
	 * 
	 * @param versionId
	 * @param plantId
	 * @param cornTypeId
	 * @return results
	 */
	public ArrayList costsReportsBrowseByCornType(int versionId, int plantId, int cornTypeId){
		ArrayList results = null;
		
		try {
			results = dao.getArrayForCostsReportsByCornType(versionId, plantId, cornTypeId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	/**
	 * Costos de perdidas de futuros por planta - mes de la versión por tipo de maíz
	 * 
	 * @param versionId
	 * @param plantId
	 * @param cornTypeId
	 * @return results
	 */
	public ArrayList costsReportsBrowseByCornTypeFutLoss(int versionId, int plantId, int cornTypeId){
		ArrayList results = null;
		
		try {
			results = dao.getArrayForCostsReportsByCornTypeFutLoss(versionId, plantId, cornTypeId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	/**
	 * Suma dos CostsPlants y regresa un tercero con la suma
	 * 
	 * @param cp1
	 * @param cp2
	 * @return cpTotal
	 */
	public CostsPlants sum(CostsPlants cp1, CostsPlants cp2){
		CostsPlants cpTotal = new CostsPlants();
		double amount;
		
		amount	= cp1.getInitialCost() + cp2.getInitialCost();
		cpTotal.setInitialCost(amount);
		
		amount	= cp1.getInitialTons() + cp2.getInitialTons();
		cpTotal.setInitialTons(amount);
		
		amount	= cpTotal.getInitialCost() * cpTotal.getInitialTons();
		cpTotal.setInitialUsd(amount);

		amount	= cp1.getIncomingCost() + cp2.getIncomingCost();
		cpTotal.setIncomingCost(amount);
		
		amount	= cp1.getIncomingTons() + cp2.getIncomingTons();
		cpTotal.setIncomingTons(amount);
		
		amount	= cpTotal.getIncomingCost() * cpTotal.getIncomingTons();
		cpTotal.setIncomingUsd(amount);

		amount	= cp1.getAvailableCost() + cp2.getAvailableCost();
		cpTotal.setAvailableCost(amount);
		
		amount	= cp1.getAvailableTons() + cp2.getAvailableTons();
		cpTotal.setAvailableTons(amount);

		amount	= cpTotal.getAvailableCost() * cpTotal.getAvailableTons();
		cpTotal.setAvailableUsd(amount);
		
		amount	= cp1.getGrindCost() + cp2.getGrindCost();
		cpTotal.setGrindCost(amount);
		
		amount	= cp1.getGrindTons() + cp2.getGrindTons();
		cpTotal.setGrindTons(amount);

		amount	= cpTotal.getGrindCost() * cpTotal.getGrindTons();
		cpTotal.setGrindUsd(amount);

		amount	= cp1.getFinalCost() + cp2.getFinalCost();
		cpTotal.setFinalCost(amount);
		
		amount	= cp1.getFinalTons() + cp2.getFinalTons();
		cpTotal.setFinalTons(amount);

		amount	= cpTotal.getFinalCost() * cpTotal.getFinalTons();
		cpTotal.setFinalUsd(amount);

		return cpTotal;
	}
	
	/**
	 * Agrega el valor de un CostsPlants al primero
	 * 
	 * @param cp1
	 * @param cp2
	 * @return cp1
	 */
	public CostsPlants add(CostsPlants cp1, CostsPlants cp2){
		cp1.setInitialCost(cp1.getInitialCost() + cp2.getInitialCost());
		cp1.setInitialTons(cp1.getInitialTons() + cp2.getInitialTons());
		cp1.setInitialUsd(cp1.getInitialCost() * cp1.getInitialTons());
		cp1.setIncomingCost(cp1.getIncomingCost() + cp2.getIncomingCost());
		cp1.setIncomingTons(cp1.getIncomingTons() + cp2.getIncomingTons());
		cp1.setIncomingUsd(cp1.getIncomingCost() * cp1.getIncomingTons());
		cp1.setAvailableCost(cp1.getAvailableCost() + cp2.getAvailableCost());
		cp1.setAvailableTons(cp1.getAvailableTons() + cp2.getAvailableTons());
		cp1.setAvailableUsd(cp1.getAvailableCost() * cp1.getAvailableTons());
		cp1.setGrindCost(cp1.getGrindCost() + cp2.getGrindCost());
		cp1.setGrindTons(cp1.getGrindTons() + cp2.getGrindTons());
		cp1.setGrindUsd(cp1.getGrindCost() * cp1.getGrindTons());
		cp1.setFinalCost(cp1.getFinalCost() + cp2.getFinalCost());
		cp1.setFinalTons(cp1.getFinalTons() + cp2.getFinalTons());
		cp1.setFinalUsd(cp1.getFinalCost() * cp1.getFinalTons());
				
		return cp1;
	}
}
